//
//  ZYTabBarController.m
//  YiLin
//
//  Created by Zyark on 2018/5/27.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYTabBarController.h"
#import "ZYNavigationController.h"
#import "ZYHomeMainVC.h"
#import "ZYMeMainVC.h"
#import "ZYIndexMainVC.h"

@interface ZYTabBarController ()<UITabBarControllerDelegate>
@property(nonatomic,strong) NSMutableArray *controllerArray;
@property(nonatomic,strong) NSMutableArray *rootControllerArray;
@end

@implementation ZYTabBarController

ZYSingletonM(Instance);

- (void)viewDidLoad{
    [super viewDidLoad];
    self.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage" object:nil];
    [self setUp];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)setUp{
    [self setUpChildViewControllers];
    [self setViewControllers:_controllerArray];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    UIColor *normalColor = [UIColor colorWithRed:149/255.0 green:149/255.0 blue:149/255.0 alpha:1];
    UIColor *selectedColor = kMainColor;
    UIFont *font = [UIFont systemFontOfSize:12.0];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:normalColor,NSForegroundColorAttributeName,font,NSFontAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:selectedColor,NSForegroundColorAttributeName,font,NSFontAttributeName, nil] forState:UIControlStateSelected];
    self.selectedIndex = 0;
}

#pragma mark - 设置子控制器
-(void)setUpChildViewControllers{
    _controllerArray = [NSMutableArray arrayWithCapacity:3];
    _rootControllerArray = [NSMutableArray arrayWithCapacity:3];
    ZYIndexMainVC *indexVC = [[ZYIndexMainVC alloc] init];
    ZYHomeMainVC *homeVC = [[ZYHomeMainVC alloc] init];
    ZYMeMainVC *meVC = [[ZYMeMainVC alloc] init];
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    [self addChildViewController:indexVC image:@"ic_tab_bar_index_unselected" seletedImage:@"ic_tab_bar_index_selected" title:[language getStringByKey:LC_TAR_BAR_INDEX]];
    [self addChildViewController:homeVC image:@"ic_tab_bar_home_unselected" seletedImage:@"ic_tab_bar_home_selected" title:[language getStringByKey:LC_TAR_BAR_HOME]];
    [self addChildViewController:meVC image:@"ic_tab_bar_me_unselected" seletedImage:@"ic_tab_bar_me_selected" title:[language getStringByKey:LC_TAR_BAR_ME]];
    
}

- (UIViewController *)addChildViewController:(UIViewController *)childController image:(NSString *)image seletedImage:(NSString *)selectedImage title:(NSString *)title
{
//    if (![FWUtils isBlankString:title])
//    {
//        childController.title = title;
//
//        NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
//        normalAttrs[NSForegroundColorAttributeName] = kAppGrayColor3;
//        [childController.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
//
//        NSMutableDictionary *selectedAtrrs = [NSMutableDictionary dictionary];
//        selectedAtrrs[NSForegroundColorAttributeName] = kAppMainColor;
//        [childController.tabBarItem setTitleTextAttributes:selectedAtrrs forState:UIControlStateSelected];
//    }
//    else
//    {
//
//    }
//    childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    // 设置图片
    [childController.tabBarItem setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [childController.tabBarItem setSelectedImage:[[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    childController.tabBarItem.title = title;
    // 导航条
    ZYNavigationController *nav = [[ZYNavigationController alloc] initWithRootViewController:childController];
    [_controllerArray addObject:nav];
    [_rootControllerArray addObject:childController];
    return childController;
}

#pragma mark - Event
-(void)changeLanguage{
    int i = 0;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    for (UIViewController *vc in _rootControllerArray) {
        switch (i) {
            case 0:
                vc.title = [language getStringByKey:LC_TAR_BAR_INDEX];
                break;
            case 1:
                vc.title = [language getStringByKey:LC_TAR_BAR_HOME];
                break;
            case 2:
                vc.title = [language getStringByKey:LC_TAR_BAR_ME];
                break;
                
            default:
                break;
        }
        i++;
    }
}

@end
